package com.avecsys.quotemanager.dao.supplier;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.avecsys.quotemanager.dao.common.BaseDAOImpl;
import com.avecsys.quotemanager.dao.common.EnumDBRecordStatus;
import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.exception.DAOException;
import com.avecsys.quotemanager.pojos.QuoteTemplateMasterPojo;
import com.avecsys.quotemanager.pojos.SupplierMasterPojo;
import com.avecsys.quotemanager.utils.TransactionStatus;

/**
 * @author Sam Sundar K
 */
public class QuoteTemplateMasterDAOImpl extends BaseDAOImpl implements QuoteTemplateMasterDAO
{
	private static final Log log = LogFactory.getLog(QuoteTemplateMasterDAOImpl.class);

	/**
	 * Method to return all Supplier Quote Templates with active status matching the templateName
	 * 
	 * @param supplierMasterPojo
	 * @param templateName
	 * @return List<QuoteTemplateMasterPojo>
	 */
	@Override
	public List<QuoteTemplateMasterPojo> findAllActiveQuoteTemplatesByName(
			final SupplierMasterPojo supplierMasterPojo, final String templateName) throws DAOException
	{
		return getHibernateTemplate().execute(new HibernateCallback<List<QuoteTemplateMasterPojo>>()
		{
			@Override
			public List<QuoteTemplateMasterPojo> doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				Criteria criteria = session.createCriteria(QuoteTemplateMasterPojo.class);
				criteria.add(Expression.eq(
						"supplierMasterPojo.supplierId",
							supplierMasterPojo.getSupplierId()));
				criteria.add(Expression.eq("templateName", templateName).ignoreCase());
				criteria.add(Expression.eq("status", EnumDBRecordStatus.ACTIVE.status()));
				return (List<QuoteTemplateMasterPojo>) criteria.list();
			}
		});
	}

	/**
	 * Method to return all Supplier Quote Templates with active status
	 * 
	 * @param supplierMasterPojo
	 * @return List<QuoteTemplateMasterPojo>
	 */
	@Override
	public List<QuoteTemplateMasterPojo> findAllSupplierQuoteTemplates(
			final SupplierMasterPojo supplierMasterPojo) throws DAOException
	{
		return getHibernateTemplate().execute(new HibernateCallback<List<QuoteTemplateMasterPojo>>()
		{
			@Override
			public List<QuoteTemplateMasterPojo> doInHibernate(Session session)
					throws HibernateException, SQLException
			{
				final Criteria criteria = session.createCriteria(QuoteTemplateMasterPojo.class);
				criteria.add(Expression.eq(
						"supplierMasterPojo.supplierId",
							supplierMasterPojo.getSupplierId()));
				criteria.add(Expression.eq("status", EnumDBRecordStatus.ACTIVE.status()));
				return (List<QuoteTemplateMasterPojo>) criteria.list();
			}
		});

	}

	/**
	 * Method to save a new Supplier Quote Template Information
	 * 
	 * @param supplierMasterPojo
	 * @return TransactionStatus
	 * @throws DAOException
	 */
	@Override
	public TransactionStatus saveQuoteTemplateMaster(
			final QuoteTemplateMasterPojo quoteTemplateMasterPojo) throws DAOException
	{
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.SAVE_SUCESS.ordinal());
		try
		{
			getHibernateTemplate().execute(new HibernateCallback<Object>()
			{
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException
				{
					// 1. Save Quote Template Master Information
					session.save(quoteTemplateMasterPojo);
					return quoteTemplateMasterPojo;
				}
			});
		}
		catch (Exception e)
		{
			log.error(e);
			transactionStatus.setStatus(EnumTransactionStatus.SAVE_FAIL.ordinal());
			throw new DAOException(e);
		}
		return transactionStatus;
	}

	/**
	 * Method to update the Supplier Quote Template Information
	 * 
	 * @param supplierMasterPojo
	 * @return TransactionStatus
	 * @throws DAOException
	 */
	@Override
	public TransactionStatus updateQuoteTemplateMaster(
			final QuoteTemplateMasterPojo quoteTemplateMasterPojo) throws DAOException
	{
		TransactionStatus transactionStatus = new TransactionStatus(
				EnumTransactionStatus.UPDATE_SUCCESS.ordinal());
		try
		{
			getHibernateTemplate().execute(new HibernateCallback<Object>()
			{
				@Override
				public Object doInHibernate(Session session) throws HibernateException, SQLException
				{
					// 2. Update Quote Template Master Information
					session.update(quoteTemplateMasterPojo);
					return quoteTemplateMasterPojo;
				}
			});
		}
		catch (Exception e)
		{
			log.error(e);
			transactionStatus.setStatus(EnumTransactionStatus.UPDATE_FAIL.ordinal());
			throw new DAOException(e);
		}
		return transactionStatus;
	}
}
